package edu.hawaii.myisern.brown.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import edu.hawaii.myisern.brown.model.MyIsernModel;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.action.DontValidate;

/**
 * Action bean for new resercher.
 * 
 * @author Randy Cox
 */
public class EditResearcherActionBean extends MyIsernActionBean {

  @Validate(required = true)
  private String name;
  private String email;
  private String organization;
  private String pictureLink;
  private String bioStatement;
  private List<String> organizations = new ArrayList<String>();;

  /**
   * Gets name.
   * 
   * @return name of researcher.
   */
  public String getName() {
    return name;
  }

  /**
   * Sets name.
   * 
   * @param name of researcher.
   */
  public void setName(String name) {
    this.name = name;
  }

  /**
   * Gets organizations.
   * 
   * @return the organizations
   */
  public List<String> getOrganizations() {
    return organizations;
  }

  /**
   * Get Email of researcher.
   * 
   * @return Email of researcher.
   */
  public String getEmail() {
    return email;
  }

  /**
   * Set Email of researcher.
   * 
   * @param email or researcher.
   */
  public void setEmail(String email) {
    this.email = email;
  }

  /**
   * @return the organization
   */
  public String getOrganization() {
    return organization;
  }

  /**
   * @param organization the organization to set
   */
  public void setOrganization(String organization) {
    this.organization = organization;
  }

  /**
   * Get Picture link of researcher.
   * 
   * @return picture link of researcher.
   */
  public String getPictureLink() {
    return pictureLink;
  }

  /**
   * Set Picture link of researcher.
   * 
   * @param pictureLink or researcher.
   */
  public void setPictureLink(String pictureLink) {
    this.pictureLink = pictureLink;
  }

  /**
   * Get Bio statement.
   * 
   * @return BioStatement of researcher.
   */
  public String getBioStatement() {
    return bioStatement;
  }

  /**
   * Set bio statement.
   * 
   * @param bioStatement or researcher.
   */
  public void setBioStatement(String bioStatement) {
    this.bioStatement = bioStatement;
  }
 
  /**
   * Loads a record on to the form ready for editing.
   * 
   * @return resolution of next page.
   */
  @DontValidate
  public Resolution preEdit() {
    Map<String, String> map = model.getOrganizations();
    List<String> list = new ArrayList<String>();
    list.addAll(map.values());
    getContext().getRequest().getSession().setAttribute("organizations", list);
    if (this.name != null) {
      this.model.fillResearcher(this, MyIsernModel.makeId(this.name));
    }
    return new RedirectResolution("/editResearcher.jsp").flash(this);
  }

  /**
   * Save record.
   * 
   * @return resolution of main menu.
   */
  public Resolution save() {
    this.model.storeResearcher(this);
    saveModel();
    return new RedirectResolution("/editResearcher.jsp").flash(this);
  }

  /**
   * Cancel and return to main menu.
   * 
   * @return resolution of main menu.
   */
  @DontValidate
  public Resolution cancel() {
    return new RedirectResolution("/mainMenu.jsp");
  }

}
